const mysql = require('mysql2/promise');
const bcrypt = require('bcryptjs');
require('dotenv').config();

async function testUser() {
  console.log('🔍 测试用户数据...');
  
  try {
    const connection = await mysql.createConnection({
      host: process.env.DB_HOST,
      port: process.env.DB_PORT,
      user: process.env.DB_USERNAME,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_DATABASE,
    });

    // 查询用户
    const [users] = await connection.execute('SELECT * FROM users WHERE username = ?', ['admin']);
    console.log('👤 用户数据:', users);

    if (users.length > 0) {
      const user = users[0];
      console.log('🔑 测试密码验证...');
      
      // 测试密码
      const testPassword = 'password';
      const isMatch = await bcrypt.compare(testPassword, user.password);
      console.log('密码验证结果:', isMatch);
      
      if (!isMatch) {
        console.log('🔧 更新密码...');
        const hashedPassword = await bcrypt.hash(testPassword, 10);
        await connection.execute('UPDATE users SET password = ? WHERE id = ?', [hashedPassword, user.id]);
        console.log('✅ 密码更新成功！');
      }
    } else {
      console.log('❌ 没有找到admin用户');
    }

    await connection.end();
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
  }
}

testUser();

